package com.agile;

public class Game {
    private int itsCurrentFrame = 0;
    private Boolean firstThrowInFrame = true;
    private Scorer scorer = new Scorer();

    public int score()
    {
        return scoreForFrame(itsCurrentFrame);
    }

    public void add(int pins)
    {
        scorer.add(pins);
        adjustCurrentFrame(pins);

    }

    // 重构 抽离方法
    public int scoreForFrame(int theFrame)
    {

        return scorer.scoreForFrame(theFrame);
    }


    public void adjustCurrentFrame(int pins)
    {

        if(lastBallInFrame(pins))
        {
            advanceFrame();
        }else{
            firstThrowInFrame = false;
        }
    }


    public boolean lastBallInFrame(int pins)
    {
        return strike(pins) || !firstThrowInFrame;
    }

    public boolean strike(int pins)
    {
        return firstThrowInFrame && (pins == 10);
    }

    public void advanceFrame()
    {
        itsCurrentFrame = Math.min(10, itsCurrentFrame+1);
    }
}
